﻿using HCQ2_Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace HCQ2_Model.SMS
{
    /// <summary>
    ///  发送短信参数
    /// </summary>
    public class SendSmsData
    {
        /// <summary>
        /// 企业ID
        /// </summary>
        public int userid { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        public string timestamp { get; set; }

        /// <summary>
        /// 签名：用户名+密码+时间戳  进行MD5结果的小写字符串：32位长度
        /// </summary>
        public string sign { get; set; }

        /// <summary>
        /// 发送的电话号码，多个电话用 , 分割
        /// </summary>
        public string mobile { get; set; }

        /// <summary>
        /// 发送内容
        /// </summary>
        public string content { get; set; }

        /// <summary>
        /// 定时发送设置定时时间，立即发送不设置
        /// </summary>
        public string sendTime { get; set; }

        /// <summary>
        /// 使用默认值
        /// </summary>
        public string action { get;  set; }

        /// <summary>
        /// 使用默认值：json
        /// </summary>
        public string rt { get; private set; }
        /// <summary>
        ///  构造
        /// </summary>
        public SendSmsData()
        {
            this.rt = "json";
            this.userid = Convert.ToInt32(AppConfigAppSetting.GetName("MessUserId"));
        }
        /// <summary>
        ///  发送
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return $"action=send&userid={userid}&timestamp={timestamp}&sign={sign}&mobile={mobile}&content={content}&sendTime={sendTime}&rt={rt}";
        }
    }
    /// <summary>
    ///  查询短信参数
    /// </summary>
    public class QuerySmsData
    {
        /// <summary>
        /// 企业ID
        /// </summary>
        public int userid { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        public string timestamp { get; set; }

        /// <summary>
        /// 签名：用户名+密码+时间戳  进行MD5结果的小写字符串：32位长度
        /// </summary>
        public string sign { get; set; }
        /// <summary>
        /// 使用默认值
        /// </summary>
        public string action { get; private set; }

        /// <summary>
        /// 使用默认值：json
        /// </summary>
        public string rt { get; private set; }
        public int statusNum { get; set; } = 4000;
        /// <summary>
        ///  构造
        /// </summary>
        public QuerySmsData()
        {
            this.rt = "json";
            this.userid = Convert.ToInt32(AppConfigAppSetting.GetName("MessUserId"));
        }
        /// <summary>
        ///  获取状态查询
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return $"action=query&userid={userid}&timestamp={timestamp}&sign={sign}&rt={rt}&statusNum={statusNum}";
        }
    }
    /// <summary>
    ///  短信平台时间戳和签名
    /// </summary>
    public class TextSmsSign
    {
        /// <summary>
        /// 时间戳
        /// </summary>
        public string timestamp { get; set; }

        /// <summary>
        /// 签名=用户名+密码+时间戳 进行 md5加密得到的小写
        /// </summary>
        public string sign { get; set; }
        /// <summary>
        /// 创建一个掌骏传媒短信平台短信发送的 时间戳和签名
        /// </summary>
        public TextSmsSign()
        {
            string name = AppConfigAppSetting.GetName("MessName"),
                pass = AppConfigAppSetting.GetName("MessPass");
            this.timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");

            this.sign = name + pass + timestamp;

            this.sign = Encrypt(sign);
        }
        /// <summary>
        ///  MD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string Encrypt(string str)
        {
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(str));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));//转化为小写的16进制
            }
            return sBuilder.ToString();
        }
    }
}